package com.example.demo2_viewpage;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ViewPageActivity extends AppCompatActivity {
    private List<Fragment> lists;
    private ViewPager viewPager;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_page);

        Toolbar toolbar =findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager=findViewById(R.id.viewpager);

        lists = new ArrayList<>();
        lists.add(new AFragment());
        lists.add(new BFragment());
        lists.add(new CFragment());
        lists.add(new DFragment());

        adapter=new MyAdapter(getSupportFragmentManager(),lists);
        viewPager.setAdapter(adapter);

        TabLayout tabLayout =findViewById(R.id.tablayout);

        tabLayout.setupWithViewPager(viewPager,true);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}